<template>
	<view class="content">
		<view v-if="list.length > 0" class="income-list">
			<view class="income-item" v-for="item in list" :key="item.id">
				<image class="income-icon" src="/static/img/income_icon.png" mode=""></image>
				<view class="income-desc">
					<text class="desc">{{item.description}}-来自-{{item.touser ? item.touser.name : ''}}</text>
					<text class="time">{{item.created_at}}</text>
				</view>
				<view class="income-count">
					<template v-if="item.money > 0">
						<text class="red">￥+{{item.money}}</text>
					</template>
					<template v-else>
						<text class="green">￥{{item.money}}</text>
					</template>
				</view>
			</view>
		</view>
		<view v-if="empty" class="empty-wrap">
			<image class="empty-icon" src="/static/img/empty.png" mode=""></image>
			<text class="empty-text">暂无账单记录</text>
		</view>
		<uni-load-more :status="status" :content-text="contentText"></uni-load-more>
	</view>
</template>

<script>
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue";
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				myInfo: {},
				empty: false,
				list: [],
				page: 1,
				status: 'more',
				contentText: {
					contentdown: '加载更多',
					contentrefresh: '加载中',
					contentnomore: '木有了~'
				},
			}
		},
		onLoad(options) {
			try {
				const myInfo = uni.getStorageSync('myInfo');
				if (myInfo) {
					this.myInfo = myInfo;
				}
			} catch (e) {
				console.log(e)
			}
		},
		onReady() {
			this.getUserIncome();
		},
		onPullDownRefresh() {
			this.page = 1;
			setTimeout(() => {
				this.getUserIncome();
			}, 500)
		},
		onReachBottom() {
			this.status = 'loading';
			this.getUserIncome();
		},
		methods: {
			getUserIncome: function () {
				this.$api.api.ajaxData({
					method: 'V1.UserIncome',
					uid: this.myInfo.id,
					token: this.myInfo.token,
					page: this.page
				}, res => {
					uni.stopPullDownRefresh();
					if (res.status) {
						if (this.page == 1) {
							this.list = res.data.list.data
							if (this.list.length == 0) {
								this.empty = true;
							}
							this.page++
							this.status = 'more'
						} else {
							if (res.data.list.data.length > 0) {
								for (let i = 0; i < res.data.list.data.length; i++) {
									this.list.push(res.data.list.data[i])
								}
								this.page++
								this.status = 'more'
							} else {
								this.status = 'noMore'
							}
						}
					} else {
						uni.showToast({
							title: res.msg,
							position: 'bottom',
							icon: 'none'
						});
					}
				}, err => {
					uni.showToast({
						title: '网络错误，请稍后再试',
						position: 'bottom',
						icon: 'none'
					});
				})
			},
		},
	}
</script>

<style lang="scss">
	@import '../../common/common.scss';
	.income-item {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		padding: 20rpx;
		margin-bottom: 1rpx;
		background-color: #FFFFFF;
		.income-icon {
			width: 80rpx;
			height: 80rpx;
			margin-right: 10rpx;
		}
		.income-desc {
			display: flex;
			flex-direction: column;
			justify-content: flex-start;
			align-items: flex-start;
			flex: 1;
			.desc {
				font-size: 30rpx;
				color: #333333;
			}
			.time {
				font-size: 24rpx;
				color: #666666;
			}
		}
		.income-count {
			text {
				font-size: 32rpx;
				&.red {
					color: #FF76A8;
				}
				&.green {
					color: #4ABA1D;
				}
			}
		}
	}
</style>
